<?php
//mq测试
namespace App\Console\Commands;

use Illuminate\Console\Command;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use Illuminate\Support\Facades\DB;

class MqSend extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mq:send';
    protected $connection;
    protected $channel;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $this->channel = $this->connection->channel();
        $this->channel->queue_declare('logs', false, false, false, false);
        //$this->channel->exchange_declare('logs','fanout',false,false,false);
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        header("Content-Type:text/html;charset=GBK");
        $date = DB::table('areas')->get();  
        //->forPage(1,10)
        if($date){
            foreach ($date as $key => $value) {
                $arr['id'] = $value->id;
                $arr['name'] = $value->name;
                $arr['code'] = $value->code;
                $data =json_encode($arr);
                
                $msg = new AMQPMessage($data,array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
                $this->channel->basic_publish($msg,'','logs');
            }
            

        }
        echo "end \n"; 
        $this->channel->close();
        $this->connection->close();
    }
}
